package talayaco.gui;

import javax.swing.JOptionPane;

import talayaco.interfaz.GestionCorralesINT;
import talayaco.utils.ErrorUtils;
import talayaco.utils.StringUtils;

public class GestionCorralesV2 extends javax.swing.JInternalFrame {

	private static final long serialVersionUID = 1L;

	public GestionCorralesV2() {
		initComponents();
	}

	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		nombreNuevoCorral = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		listadotipoNuevoCorral = new javax.swing.JComboBox();
		crearCorral = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();
		jPanel2 = new javax.swing.JPanel();
		jLabel4 = new javax.swing.JLabel();
		listadoTipoCorral = new javax.swing.JComboBox();
		guardarCambios = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jLabel3 = new javax.swing.JLabel();
		listadotipoNuevoCorral1 = new javax.swing.JComboBox();

		this.setTitle("Gestion de Corrales");

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);
		tipoCorral1 = "";
		tipoCorral2 = "";
		nombreCorral = "";

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Nuevo Corral",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Tahoma", 0, 11))); // NOI18N

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jLabel1.setText("Nombre");

		nombreNuevoCorral.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N

		jLabel2.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jLabel2.setText("Tipo");

		try {
			tipoCorrales = GestionCorralesINT.getTipoCorrales();
			listadotipoNuevoCorral.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
			listadotipoNuevoCorral
					.setModel(new javax.swing.DefaultComboBoxModel(tipoCorrales));
			listadotipoNuevoCorral
					.addItemListener(new java.awt.event.ItemListener() {
						public void itemStateChanged(
								java.awt.event.ItemEvent evt) {
							listadotipoNuevoCorralItemStateChanged(evt);
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

		crearCorral.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		crearCorral.setText("Crear Corral");
		crearCorral.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				crearCorralActionPerformed(evt);
			}
		});

		jButton1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jButton1.setText("Cancelar");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																jLabel2,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jLabel1,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																listadotipoNuevoCorral,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																nombreNuevoCorral,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																87,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																crearCorral,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																142,
																Short.MAX_VALUE)
														.addComponent(
																jButton1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																142,
																Short.MAX_VALUE))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabel1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																19,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																nombreNuevoCorral,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																crearCorral))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabel2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																19,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																listadotipoNuevoCorral,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																21,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jButton1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																14,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL,
				new java.awt.Component[] { crearCorral, jButton1 });

		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Modificar o Eliminar Corral",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Tahoma", 0, 11))); // NOI18N

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jLabel4.setText("Tipo");

		listadoTipoCorral.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		listadoTipoCorral.setModel(new javax.swing.DefaultComboBoxModel(
				tipoCorrales));
		listadoTipoCorral.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				listadoTipoCorralItemStateChanged(evt);
			}
		});

		guardarCambios.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		guardarCambios.setText("Guardar Cambios");
		guardarCambios.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				guardarCambiosActionPerformed(evt);
			}
		});

		jButton2.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jButton2.setText("Cancelar");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jButton3.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jButton3.setText("Eliminar");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jLabel3.setText("Nombre");

		try {
			nombreCorrales = GestionCorralesINT.getNombreCorrales();
			listadotipoNuevoCorral1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
			listadotipoNuevoCorral1
					.setModel(new javax.swing.DefaultComboBoxModel(
							nombreCorrales));
			listadotipoNuevoCorral1
					.addItemListener(new java.awt.event.ItemListener() {
						public void itemStateChanged(
								java.awt.event.ItemEvent evt) {
							listadoNombreCorralItemStateChanged(evt);
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel3)
																		.addGap(18,
																				18,
																				18))
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel4,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGap(34,
																				34,
																				34)))
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																listadoTipoCorral,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																listadotipoNuevoCorral1,
																0, 85,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				78,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton3))
														.addComponent(
																guardarCambios,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabel3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																17,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																listadotipoNuevoCorral1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																guardarCambios))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabel4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																19,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																listadoTipoCorral,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																21,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jButton2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																21,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jButton3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																21,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														jPanel1,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jPanel2,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPanel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	private void crearCorralActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			if (StringUtils.isNullOrBlank(nombreNuevoCorral.getText())) {
				JOptionPane.showMessageDialog(this, ErrorUtils.CORRAL_VACIO,
						"ERROR", JOptionPane.ERROR_MESSAGE);
			} else if (nombreNuevoCorral.getText().length() > 100) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.NOMBRE_CORRAL_MAX, "ERROR",
						JOptionPane.ERROR_MESSAGE);

			} else if (GestionCorralesINT.existeCorral(nombreNuevoCorral
					.getText())) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.CORRAL_EXISTENTE, "ERROR",
						JOptionPane.ERROR_MESSAGE);

			} else if (StringUtils.isNullOrBlank(tipoCorral1)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_TIPO_CORRAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else {
				GestionCorralesINT.insertCorral(nombreNuevoCorral.getText(),
						tipoCorral1, this);
			}

		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void listadotipoNuevoCorralItemStateChanged(
			java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un tipo") {
				tipoCorral1 = "";
			} else {
				tipoCorral1 = evt.getItem().toString();
			}
		}

	}

	private void listadoNombreCorralItemStateChanged(
			java.awt.event.ItemEvent evt) {
		try {
			if (evt.getStateChange() == 1) {
				if (evt.getItem() == "Seleccione Corral") {
					nombreCorral = "";
					String tipo = GestionCorralesINT.getTipoByNombre(evt
							.getItem().toString());
					int indice = 0;
					for (int i = 0; i < tipoCorrales.length; i++)
						if (tipoCorrales[i].equals(tipo))
							indice = i;

					listadoTipoCorral.setSelectedIndex(indice);
				} else {
					nombreCorral = evt.getItem().toString();
					String tipo = GestionCorralesINT
							.getTipoByNombre(nombreCorral);
					int indice = 0;
					for (int i = 0; i < tipoCorrales.length; i++)
						if (tipoCorrales[i].equals(tipo))
							indice = i;

					listadoTipoCorral.setSelectedIndex(indice);

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void listadoTipoCorralItemStateChanged(java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un tipo")
				tipoCorral2 = "";
			else {
				tipoCorral2 = evt.getItem().toString();
			}
		}
	}

	private void guardarCambiosActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			if (StringUtils.isNullOrBlank(nombreCorral))
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_NOMBRE_CORRAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			else if (StringUtils.isNullOrBlank(tipoCorral2))
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_TIPO_CORRAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			else {
				GestionCorralesINT.modificarCorral(nombreCorral, tipoCorral2,
						this);
			}

		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		if (JOptionPane.showConfirmDialog(this, ErrorUtils.CANCELAR,
				"CANCELAR", JOptionPane.YES_NO_OPTION) == 0)
			this.dispose();
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		if (JOptionPane.showConfirmDialog(this, ErrorUtils.CANCELAR,
				"CANCELAR", JOptionPane.YES_NO_OPTION) == 0)
			this.dispose();
	}

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			if (StringUtils.isNullOrBlank(nombreCorral)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_NOMBRE_CORRAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);

			} else if (GestionCorralesINT.existenDatosCorral(nombreCorral)) {
				JOptionPane.showMessageDialog(this,
						ErrorUtils.CORRAL_NECESARIO, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			} else {
				GestionCorralesINT.deleteCorral(nombreCorral, this);
			}

		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

	}

	// Variables declaration - do not modify
	private javax.swing.JButton crearCorral;
	private javax.swing.JButton guardarCambios;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JComboBox listadotipoNuevoCorral1;
	private javax.swing.JComboBox listadoTipoCorral;
	private javax.swing.JComboBox listadotipoNuevoCorral;
	private javax.swing.JTextField nombreNuevoCorral;
	private String tipoCorral1;
	private String tipoCorral2;
	private String[] tipoCorrales;
	private String[] nombreCorrales;
	private String nombreCorral;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton3;
	// End of variables declaration
}
